<?php
namespace jy_qqdian;
defined('BY_JYA') or exit('error');
/*
	分享员接口
	https://developers.weixin.qq.com/doc/store/API/sharer/bindsharer.html
*/
class sharer extends \plugin{
	/*
		邀请分享员
	*/
	public function sharerewm(){
		$basic=$this->plugin_setting('basic');
		if($basic['sharerewm'] && $basic['sharetime']>time()-14.6*24*3600){
			return $basic['sharerewm'];
		}
		$res=$this->model('api')->sharerewm();
		$sharerewm=cfc('files')->source_save($res['qrcode_img'],'image','jpg','邀请分享员二维码');
		$basic['sharerewm']=$sharerewm;
		$basic['sharetime']=time();
		$this->plugin_setting('basic',$basic);
		return $basic['sharerewm'];
	}
	//获取分享员
	public function upsharer(){
		$list=$this->model('api')->get_sharer_list();
		$basic=$this->plugin_setting('basic');
		foreach($list as $item){
			$this->upone($item,$basic);
		}
		return true;
	}
	//添加分享员
	public function upone($item=array(),$basic=array()){
		$isadd=pdo_get('jy_qqdian_sharer',array('openid'=>$item['openid'],'uniacid'=>$_SESSION['uniacid']));
		$data=array(
				'openid'=>trim($item['openid']),
				'nickname'=>trim($item['nickname']),
				'bind_time'=>trim($item['bind_time']),
				'sharer_type'=>trim($item['sharer_type']),
				'unionid'=>trim($item['unionid']),
		);
		$basic=$basic?:$this->plugin_setting('basic');
		$data['appid']=$basic['appid'];
		$data['mid']=$this->upfans($data);
		$id=intval($isadd['id']);
		if($id){
			//
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=time();
			pdo_insert('jy_qqdian_sharer',$data);
		}
		return true;
	}
	//获取订单
	public function uporderlist(){
		$list=$this->model('api')->get_sharer_order_list();
		$basic=$this->plugin_setting('basic');
		foreach($list as $item){
			$this->uponeorder($item,$basic);
		}
		return true;
	}
	//获取订单
	public function addorder($data=array()){
		$order=$this->model('api')->get_orderone($data);
		$order_id=$this->mode('order')->add($order);
		return $order_id;
	}
	//添加订单
	public function uponeorder($item=array(),$basic=array()){
		$isadd=pdo_get('jy_qqdian_sharer_order',array('openid'=>$item['openid'],'uniacid'=>$_SESSION['uniacid']));
		$data=array(
			'openid'=>trim($item['openid']),
			'price'=>trim($item['price']),
			'order_id'=>trim($item['order_id']),
			'share_scene'=>trim($item['share_scene']),
			'sharer_type'=>trim($item['sharer_type']),
			'sharer_openid'=>trim($item['sharer_openid']),
			'sku_id'=>trim($item['sku_id']),
			'product_id'=>trim($item['product_id']),
			'from_wecom'=>trim($item['from_wecom']),
			'finder_scene_info'=>ijson_encode($item['finder_scene_info']),
		);
		$basic=$basic?:$this->plugin_setting('basic');
		$data['fromuser']=$this->upfans($data,$sharer_openid);
		$data['mid']=$this->upfans($data);
		$id=intval($isadd['id']);
		if($id){
			if(!$isadd['myorderid']){
				$myorderid=$this->addorder($data);
				pdo_update('jy_qqdian_sharer_order',array('myorderid'=>$myorderid),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}else{
			$data['myorderid']=$this->addorder($data);
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=time();
			pdo_insert('jy_qqdian_sharer_order',$data);
		}
		return true;
	}
	public function upfans($params=array(),$sharer_openid=''){
		$params['openid']=$sharer_openid?:$params['openid'];
		$fans=pdo_get('core_fans',array('type'=>array(1,2),'openid'=>$params['openid'],'uniacid'=>$_SESSION['uniacid']),array('id','mid','nickname','avatar','sex'));
		if($fans){
				if(!$mid && $fans['mid']){
					$mid= $fans['mid'];
					$member=pdo_get('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('id','onemid'));
					if($member['id']){
						$mid=$member['onemid']>0?$member['onemid']:$member['id'];
					}else{
						$mid=0;
					}
				}
		}else{
				$fansdata=array(
					'uniacid'=>$_SESSION['uniacid'],
					'openid'=>$params['openid'],
					'nickname'=>$params['nickname'],
					'type'=>1,
					'createtime' => TIMESTAMP,
				);				
				if($mid){
					$fansdata['mid']=$mid;
				}
				if($result['unionid']){
					$fansdata['unionid']=$result['unionid'];
				}
				pdo_insert('core_fans',$fansdata);
		}
		if($mid){
			if($params['fromuser']){
				$fromuser=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid),'fromuser');
				if(!$fromuser){
					pdo_update('core_members',array('fromuser'=>$params['fromuser']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid));
				}
			}
			return $mid;
		}else{
				//自动注册会员
				$new_member=array(
					'uniacid'=>$_SESSION['uniacid'],
					'fromuser'=>$params['fromuser'],
					'from_os'=>1,
					'createtime'=>TIMESTAMP,
					'unionid'=>$params['unionid'],
					'nickname'=>$fans['nickname']?:'',//留空好判断获取
					'avatar'=>$fans['avatar'],
					'sex'=>$fans['sex'],
					'username'=>'wxapp_'.random(10),
				);
				$uid=model('core/member')->register($new_member);
				$params=array('type'=>array(1,2),'openid'=>$params['openid'],'uniacid'=>$_SESSION['uniacid']);
				pdo_update('core_fans',array('mid'=>$uid),$params);
				return $uid;
		}
	}
}